package top.wherewego.vnt.activity;

import android.os.Build;
import android.view.View;
import android.widget.CompoundButton;

import com.kongzue.baseframework.BaseBindingActivity;
import com.kongzue.baseframework.interfaces.DarkStatusBarTheme;
import com.kongzue.baseframework.util.JumpParameter;
import com.kongzue.dialogx.dialogs.BottomMenu;
import com.kongzue.dialogx.dialogs.PopTip;
import com.kongzue.dialogx.dialogs.TipDialog;
import com.kongzue.dialogx.dialogs.WaitDialog;
import com.kongzue.dialogx.interfaces.DialogXRunnable;
import com.kongzue.dialogx.interfaces.OnMenuItemClickListener;

import top.wherewego.vnt.APP;
import top.wherewego.vnt.R;
import top.wherewego.vnt.bean.Conf;
import top.wherewego.vnt.databinding.ActivityAddBinding;


@DarkStatusBarTheme(true)
public class AddActivity extends BaseBindingActivity<ActivityAddBinding> {
    String regexUrlPort = "^[a-zA-Z0-9.-]+(:[0-9]+)?$";
    String regexIpPort = "^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?):\\d{1,5}$";


    @Override
    public void initDatas(JumpParameter parameter) {

    }

    @Override
    public void setEvents() {
        String name = Build.BRAND + "-" + Build.MODEL;
        binding.addName.setText(name.replace(" ", "-"));
        binding.addEncryption.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                binding.addLayoutEncryption.setVisibility(b ? View.VISIBLE : View.GONE);
            }
        });
        binding.addModel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BottomMenu.show(getResources().getTextArray(R.array.model_items))
                        .setTitle("加密模式")
                        .setMessage("加密模式(默认aes_gcm),可选值aes_gcm/aes_cbc/aes_ecb/sm4_cbc")
                        .setOnMenuItemClickListener(new OnMenuItemClickListener<BottomMenu>() {
                            @Override
                            public boolean onClick(BottomMenu dialog, CharSequence text, int index) {
                                binding.addModel.setText(text.toString());
                                return false;
                            }
                        });
            }
        });
        binding.addPar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BottomMenu.show(getResources().getTextArray(R.array.par_items))
                        .setTitle("任务并行度")
                        .setMessage("任务并行度(必须为正整数),默认值为1")
                        .setOnMenuItemClickListener(new OnMenuItemClickListener<BottomMenu>() {
                            @Override
                            public boolean onClick(BottomMenu dialog, CharSequence text, int index) {
                                binding.addPar.setText(text.toString());
                                return false;
                            }
                        });
            }
        });
        binding.addPunch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BottomMenu.show(getResources().getTextArray(R.array.punch_items))
                        .setTitle("打洞路径")
                        .setMessage("取值ipv4/ipv6/all,ipv4表示仅使用ipv4打洞")
                        .setOnMenuItemClickListener(new OnMenuItemClickListener<BottomMenu>() {
                            @Override
                            public boolean onClick(BottomMenu dialog, CharSequence text, int index) {
                                binding.addPunch.setText(text.toString());
                                return false;
                            }
                        });
            }
        });
        binding.addChannel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BottomMenu.show(getResources().getTextArray(R.array.punch_items))
                        .setTitle("使用通道")
                        .setMessage("使用通道 relay/p2p/all,默认两者都使用")
                        .setOnMenuItemClickListener(new OnMenuItemClickListener<BottomMenu>() {
                            @Override
                            public boolean onClick(BottomMenu dialog, CharSequence text, int index) {
                                binding.addChannel.setText(text.toString());
                                return false;
                            }
                        });
            }
        });
        binding.addConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (checkField(binding.addConName.getText().toString(), "请填写配置名称")) return;
                if (checkField(binding.addToken.getText().toString(), "请填写Token")) return;
                Conf conf = new Conf();
                conf.setConName(binding.addConName.getText().toString());
                conf.setToken(binding.addToken.getText().toString());
                //服务器
                if (!binding.addServer.getText().toString().equals("")) {
                    if (binding.addServer.getText().toString().split(":").length == 2) {
                        conf.setServer(binding.addServer.getText().toString());
                    } else {
                        PopTip.show(R.drawable.ic_error, "服务器地址格式有误");
                        return;
                    }
                }
                //设备名称
                if (!binding.addName.getText().toString().equals("")) {
                    conf.setName(binding.addName.getText().toString());
                }
                //id
                if (!binding.addId.getText().toString().equals("")) {
                    conf.setId(binding.addId.getText().toString());
                }
                //stun服务器
                if (!binding.addStunServer.getText().toString().equals("")) {
                    conf.setStunServer(binding.addServer.getText().toString().split("\n"));
                }
                //inip
                if (!binding.addInIp.getText().toString().equals("")) {
                    conf.setInIp(binding.addInIp.getText().toString().split("\n"));
                }
                //outip
                if (!binding.addOutIp.getText().toString().equals("")) {
                    conf.setOutIp(binding.addOutIp.getText().toString().split("\n"));
                }
                //加密
                conf.setEncryption(binding.addEncryption.isChecked());
                if (!binding.addPassword.getText().toString().equals("")) {
                    conf.setPassword(binding.addPassword.getText().toString());
                }
                //加密方式
                if (!binding.addModel.getText().toString().equals("")) {
                    conf.setModel(binding.addModel.getText().toString());
                }
                //tcp
                conf.setTcp(binding.addTcp.isChecked());
                //ip
                if (!binding.addIp.getText().toString().equals("")) {
                    conf.setIp(binding.addIp.getText().toString());
                }
                //par
                if (!binding.addPar.getText().toString().equals("")) {
                    conf.setPar(binding.addPar.getText().toString());
                }
                //finger
                conf.setFinger(binding.addFinger.isChecked());
                //取值ipv4/ipv6/all,ipv4表示仅使用ipv4打洞
                if (!binding.addPunch.getText().toString().equals("") || !binding.addPunch.getText().toString().equals("all")) {
                    conf.setPunch(binding.addPunch.getText().toString());
                }
                //关闭内置代理
                conf.setNoProxy(binding.addProxy.isChecked());
                //优先使用通道
                conf.setFirstLatency(binding.addLatency.isChecked());
                //使用通道
                if (!binding.addChannel.getText().toString().equals("") || !binding.addChannel.getText().toString().equals("all")) {
                    conf.setUseChannel(binding.addChannel.getText().toString());
                }
                //模拟丢包
                if (!binding.addLoss.getText().toString().equals("")) {
                    conf.setPacketLoss(binding.addLoss.getText().toString());
                }
                //模拟延迟
                if (!binding.addDelay.getText().toString().equals("")) {
                    conf.setPacketDelay(binding.addDelay.getText().toString());
                }
                APP.addConfList(conf);
                TipDialog.show("添加成功!", WaitDialog.TYPE.SUCCESS)
                        .onDismiss(new DialogXRunnable<WaitDialog>() {
                            @Override
                            public void run(WaitDialog dialog) {
                                me.finish();
                            }
                        });

            }
        });
        binding.addBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                me.finish();
            }
        });

    }


    private boolean checkField(String field, String msg) {
        if (field == null || field.equals("")) {
            PopTip.show(R.drawable.ic_error, msg);
            return true;
        } else {
            return false;
        }
    }
}